<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_summaryAttributesJobService"></title>
    <script type="text/javascript" include="jquery,bootstrap,widgets" src="../js/include-web.js"></script>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="width: 100%;height:100%"></div>
    <script type="text/javascript" include="echarts" src="../../dist/leaflet/include-leaflet.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "http://114.115.213.31:8090";
        var chartControl, processingUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs",
            mapURL = host + "/iserver/services/map-world/rest/maps/World",
            map = L.map('map', {
                crs: L.CRS.EPSG4326,
                center: [40.75, -73.95],
                maxZoom: 18,
                zoom: 12
            }),
            info = L.control({
                position: 'topright'
            });
        L.supermap.tiledMapLayer(mapURL).addTo(map);

        info.onAdd = function () {
            var popup = L.DomUtil.create('div');
            popup.style.width = '300px';
            popup.innerHTML = "<div class='panel panel-default'>" +
                "<div class='panel-heading'>" +
                "<h3 class='panel-title'>" + resources.title_summaryAttributesJobService + "</h3>" +
                "</div>" +
                "<div class='panel-body'>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_inputData + "<span title='" + resources.text_requiredField +
                "' style='color: red;'> * </span>　</span>" +
                "<input id='datasetName' type='text' class='form-control' value='samples_processing_newyorkZone_R'/>" +
                "</div><p></p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_groupField + "</span>" +
                "<input id='groupField' type='text' class='form-control' value='borough'/>" +
                "</div><p></p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_attributeField + "</span>" +
                "<input id='attributeField' type='text' class='form-control' value='LocationID'/>" +
                "</div><p></p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>" + resources.text_statisticModes + "</span>" +
                "<input id='statisticModes' type='text' class='form-control' value='sum'/>" +
                "</div><p></p>" +
                "<div align='right'>" +
                "<input type='button' id='btn' class='btn btn-primary' value='" + resources.btn_summary + "'/>" +
                "</div></div></div>";
            handleMapEvent(popup, this._map);
            return popup;
        };
        info.addTo(map);

        var processingService = new L.supermap.processingService(processingUrl, {
            withCredentials: window.isLocal
        });
        SuperMap.SecurityManager.registerToken(processingUrl, window.exampleToken);


        $('#btn').on('click', function () {
            if ($('#msg_container')[0]) {
                $('#msg_container').remove();
            }
            widgets.loader.showLoader();
            if (map && chartControl) {
                map.removeControl(chartControl);
            }
            var summaryAttributesJobsParameter = new SuperMap.SummaryAttributesJobsParameter({
                datasetName: $('#datasetName').val(),
                groupField: $('#groupField').val(),
                attributeField: $('#attributeField').val(),
                statisticModes: $('#statisticModes').val()
            });
            processingService.addSummaryAttributesJob(summaryAttributesJobsParameter, function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTDATA') {
                        var dataUrl = info.serviceAddress + '/data';
                        var sqlParam = new SuperMap.GetFeaturesBySQLParameters({
                            queryParameter: {
                                name: "analystResult@UntitledDatasource",
                                attributeFilter: "SMID > 0"
                            },
                            datasetNames: ["UntitledDatasource:analystResult"]
                        });
                        L.supermap
                            .featureService(dataUrl)
                            .getFeaturesBySQL(sqlParam, function (serviceResult) {
                                result = serviceResult.result.features.features;
                                var names = [];
                                var data1 = [];
                                var data2 = [];
                                for (var i = 0; i < result.length; i++) {
                                    names.push(result[i].properties.BOROUGH);
                                    data1.push(parseFloat(result[i].properties.RECORDCOUNT));
                                    data2.push(parseFloat(result[i].properties.SUM_LOCATIONID));
                                }
                                option = {
                                    backgroundColor: '#0E2A43',
                                    legend: {
                                        bottom: 20,
                                        textStyle: {
                                            color: '#fff',
                                        },
                                        data: ['RECORDCOUNT', 'SUM_LOCATIONID']
                                    },
                                    grid: {
                                        left: '3%',
                                        right: '4%',
                                        bottom: '20%',
                                        containLabel: true
                                    },
                                    tooltip: {
                                        show: "true",
                                        trigger: 'axis',
                                        axisPointer: { // 坐标轴指示器，坐标轴触发有效
                                            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                                        }
                                    },
                                    xAxis: {
                                        type: 'value',
                                        axisTick: {
                                            show: false
                                        },
                                        axisLine: {
                                            show: false,
                                            lineStyle: {
                                                color: '#fff',
                                            }
                                        },
                                        splitLine: {
                                            show: false
                                        },
                                    },
                                    yAxis: [{
                                            type: 'category',
                                            data: names,
                                            axisTick: {
                                                show: false
                                            },
                                            axisLine: {
                                                show: true,
                                                lineStyle: {
                                                    color: '#fff',
                                                }
                                            }
                                        },
                                        {
                                            type: 'category',
                                            data: names,
                                            axisLine: {
                                                show: false
                                            },
                                            axisTick: {
                                                show: false
                                            },
                                            axisLabel: {
                                                show: false
                                            },
                                            splitArea: {
                                                show: false
                                            },
                                            splitLine: {
                                                show: false
                                            }
                                        }
                                    ],
                                    series: [{
                                            name: 'RECORDCOUNT',
                                            type: 'bar',
                                            itemStyle: {
                                                normal: {
                                                    show: true,
                                                    color: '#5de3e1',
                                                    barBorderRadius: 50,
                                                    borderWidth: 0,
                                                    borderColor: '#333',
                                                }
                                            },
                                            data: data1,
                                            barGap: '0%',
                                            barCategoryGap: '50%'
                                        },
                                        {
                                            name: 'SUM_LOCATIONID',
                                            type: 'bar',
                                            yAxisIndex: 1,

                                            itemStyle: {
                                                normal: {
                                                    show: true,
                                                    color: '#277ace',
                                                    barBorderRadius: 50,
                                                    borderWidth: 0,
                                                    borderColor: '#333',
                                                }
                                            },
                                            data: data2,
                                            barGap: '0%',
                                            barCategoryGap: '50%'
                                        }
                                    ]
                                };
                                var div = L.DomUtil.create('div');
                                var chart = echarts.init(div, '', {
                                    width: 400,
                                    height: 300
                                });
                                chart.setOption(option);
                                chartControl = L.control({
                                    position: 'topleft'
                                });
                                chartControl.onAdd = function (map) {
                                    return chart.getDom();
                                };
                                chartControl.addTo(map);
                            });
                        widgets.loader.removeLoader();
                    }
                });
            });
        });


        function handleMapEvent(div, map) {
            if (!div || !map) {
                return;
            }
            div.addEventListener('mouseover', function () {
                map.dragging.disable();
                map.scrollWheelZoom.disable();
                map.doubleClickZoom.disable();
            });
            div.addEventListener('mouseout', function () {
                map.dragging.enable();
                map.scrollWheelZoom.enable();
                map.doubleClickZoom.enable();
            });
            $("#model").on('shown.bs.modal', function () {
                map.dragging.disable();
                map.scrollWheelZoom.disable();
                map.doubleClickZoom.disable();
            });
            $("#model").on('hidden.bs.modal', function () {
                map.dragging.enable();
                map.scrollWheelZoom.enable();
                map.doubleClickZoom.enable();
            })
        }
    </script>
</body>

</html>